﻿/**

* @author admin

*/


$(document).ready(onLoadComplete);
 
 
 
var venues;
var map;
var geocoder ;
var markers = new Array();
function onLoadComplete(evn){
		geocoder = new google.maps.Geocoder();
		getCurrentLocation();
		checkDisplaySize();
		
}
function showNav(){
	var nav = document.getElementsByTagName("nav")[0];
	var btn = document.getElementById("listBtn");
	nav.style.display="inline-block";
	btn.value="<"
	btn.onclick=hideNav;
}
function hideNav(){
	var nav = document.getElementsByTagName("nav")[0];
	var btn = document.getElementById("listBtn");
	nav.style.display="none";
	btn.value=">"
	btn.onclick=showNav;
}
function checkDisplaySize(){
	/*var wd = window.screen.width;
	var ht = window.screen.height;
	var dim =wd * ht ;
	alert(dim);
	if(wd > 420){*/
		changeHeader();
	//}
}
function changeHeader(){
	document.getElementById("range").setAttribute("style","top:5px;");
}
function getCurrentLocation(){
	if(navigator.geolocation) {
    browserSupportFlag = true;
    navigator.geolocation.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
      map.setCenter(initialLocation);
    }, function() {
      handleNoGeolocation(browserSupportFlag);
    });
  // Try Google Gears Geolocation
  } else if (google.gears) {
    browserSupportFlag = true;
    var geo = google.gears.factory.create('beta.geolocation');
    geo.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
      map.setCenter(initialLocation);
    }, function() {
      handleNoGeoLocation(browserSupportFlag);
    });
  // Browser doesn't support Geolocation
  } else {
    browserSupportFlag = false;
    handleNoGeolocation(browserSupportFlag);
  }
  
  showMap( );

		
	
}
function handleNoGeolocation(errorFlag) {
	initialLocation = new google.maps.LatLng( 40.714623,-74.006605) ;
    if (errorFlag == true) {
      alert("Geolocation service failed. We've placed you in New York.");
    
    } else {
      alert("Your browser doesn't support geolocation. We've placed you in New York.");
      
    }
    map.setCenter(initialLocation);
  }
  function selectMarkerImageSize(chkins){
  	var name = "";
  		switch(chkins.toString().length){
  			case 2 :
  				name="marker.png";
  				break;
  			case 1 :
  				name="marker.png";
  				break;
  			case 3 :
  				name="marker3D.png";
  				break;
  			case 4 :
  				name="marker4D.png";
  				break;
  		}
  		return name;
  	}
  	
function loadSideBar(){
	var navMarkers = document.getElementById("markerInfo");
	var htmlStr = "";
	
	for (var i = 0; i < venues.length ; i++){
		var v = venues[i];
		var chkins =  v.stats.checkinsCount;
	 	
	 	 
		
		var htmlIcon = "<li><img src='assets/icons/"+selectMarkerImageSize(chkins)+"'/><p class='checkinTotal  nav"+selectMarkerLabelClass(chkins)+"'>"+ chkins +" </p>"; 
		var htmlName = "";
		var htmlAddress = "";
		
		
		if(v.name){
			htmlName = "<p class='name'> "+v.name+"</p>";		
		}
		if(v.location.address){
			htmlAddress = 	"<p class='address'>"+v.location.address+"</p></li>";
		}
		
		
		 
		htmlStr += htmlIcon + htmlName + htmlAddress ;

	}


	navMarkers.innerHTML = htmlStr;
	
}
function loadMarkers(){
	var center = map.getCenter();
	var urlstr = "https://api.foursquare.com/v2/venues/search?ll="+center.lat()+", "+center.lng()+"&oauth_token=NOLFUGXM1CDFHLYYCO3LL2HT4MPIB0HD4FNDUROLMY5TITXT&v=20120329";
	
	
	$.ajax({
  		url: urlstr,
  		context: document.body,
  		success:setMarkers
		})
}
function setMarkers(response){
 	
 	if(typeof(response) == typeof("")){
 		var js= $.parseJSON(response);		
 		venues = js.response.venues ;
 	}else{
 		venues = response.response.venues ;
 	}
	

	
	
	setVenuesOnMap();
	loadSideBar();
	
	setAutoComplete();
}
function venueOnMarker(v){
	var pt = v.location.lat +"-"+v.location.lng;

	
	for(var i = 0 ; i < markers.length ; i ++ ){
		if(pt === markers[i]){

		}
	}

	
}
function setVenuesOnMap(){

	var infowindow = new google.maps.InfoWindow();	    
	var marker;
	for (var i = 0; i < venues.length ; i++){
		var v = venues[i];
		var chkins =  v.stats.checkinsCount;
		venueOnMarker(v);
		markers.push(v.location.lat+"-"+v.location.lng);
		var latlng = new google.maps.LatLng( v.location.lat,v.location.lng);
		
		 marker = new google.maps.Marker({    
	 	     position: latlng,    
 	    	 map: map  ,
  	    	 title: v.name,
  		  	 icon: new google.maps.MarkerImage("assets/icons/"+selectMarkerImageSize(chkins))
	    });  

       google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent("11111");
          infowindow.open(map, marker);
          highlightOnNav(marker);

        }
      })(marker, i));
	
		 

		var label = new CheckinMarkup(latlng,chkins, map);

	 
	     label.show();
   		 
	}

}

function highlightOnNav(){
	var navMarkers = document.getElementById("markerInfo");
		
}
function toggleBounce(marker) {

  if (marker.getAnimation() != null) {
    marker.setAnimation(null);
  } else {
    marker.setAnimation(google.maps.Animation.BOUNCE);
  }
}

function showMap( ){
 
    var myOptions = {
      zoom: 18,
      
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      mapMaker:true
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    google.maps.event.addListener(map, 'center_changed', (
   		function() {   
   			google.maps.event.addListener(map,'tilesloaded',(
   			function (){
   				loadMarkers();
   			}));   
    	}));
    	
    	 
}


function doSearch(){
	var val = document.getElementById("searchTxt").value;
	alert(val);
	
}
function setAutoComplete() {
    $("#searchTxt").autocomplete({
      //This bit uses the geocoder to fetch address values
      source: function(request, response) {
        geocoder.geocode( {'address': request.term }, function(results, status) {
          response($.map(results, function(item) {
            return {
              label:  item.formatted_address,
              value: item.formatted_address,
              latitude: item.geometry.location.lat(),
              longitude: item.geometry.location.lng()
            }
          }));
        })
      },
      //This bit is executed upon selection of an address
      select: function(event, ui) {
      	var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);
        map.setCenter(location);
      }
    });
 }